package network.tcp.nio.http;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.HashMap;

/**
 * @program: bio
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-27 19:35
 **/
public class HttpRequest {
    private String method;
    private String requestURI;
    private String version;
    private HashMap<String, String> headers = new HashMap<>();

    @Override
    public String toString() {
        return "HttpRequest{" +
                "method='" + method + '\'' +
                ", requestURI='" + requestURI + '\'' +
                ", version='" + version + '\'' +
                ", headers=" + headers +
                '}';
    }

    public HashMap<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(HashMap<String, String> headers) {
        this.headers = headers;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getRequestURI() {
        return requestURI;
    }

    public void setRequestURI(String requestURI) {
        this.requestURI = requestURI;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void parse(SelectionKey selectionKey) {
        //得到可用的channel,进行通信
        try {
            SocketChannel client = (SocketChannel) selectionKey.channel();
            if (client != null) {
                //创建缓冲区
                StringBuilder sb = new StringBuilder();
                ByteBuffer dst = ByteBuffer.allocate(1024);
                int len;
                while ((len = client.read(dst)) > 0) {
                    dst.flip();
                    sb.append(new String(dst.array(), 0, len));
                    dst.clear();
                }
                parseHttpRequest(sb);
                //不要关闭client
            }
            //设置非阻塞
            //
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源   加入到 try的小括号中

        }
    }

    private void parseHttpRequest(StringBuilder sb) {
        String s = sb.toString();
        if (!(s == null || "".equals(s))) {
            //请求行,请求头,请求空行,请求体
            String[] split = s.split("\r\n");

            //GET https://www.fiddler2.com/UpdateCheck.aspx?isBeta=False HTTP/1.1

            //3.获取请求行
            String httpRequestLine = split[0];//GET / HTTP/1.1
            // 4.按照空格进行切割,得到请求行中的三部分
            String[] httpRequestInfo = httpRequestLine.split(" ");
            this.method = httpRequestInfo[0];
            this.requestURI = httpRequestInfo[1];
            this.version = httpRequestInfo[2];
            for (int i = 1; i < split.length; i++) {
                //都是请求头了
                //Pragma: no-cache
                // Host: www.fiddler2.com
                // Accept-Language: zh-CN
                // Referer: http://fiddler2.com/client/5.0.20202.18177
                // Accept-Encoding: gzip, deflate
                // Connection: close
                String header = split[i];
                String[] keyValue = header.split(": ");
                this.headers.put(keyValue[0], keyValue[1]);
            }
            System.out.println("http解析后数据: ");
            System.out.println(this);
        }
    }
}
